"""
    将requests中常用的方法进行封装
        保持会话，自动管理cookies
        打印相关消息，接口地址、参数、返回值
        异常处理，避免程序异常中断
"""
import requests

from jinrongAutoTest.caw.log import Logger


#封装
class BaseRequests:
    def __init__(self):
        self.session = requests.session()   #创建一个session对象

    def get(self,url,**kwargs): #发起get请求并返回 Response响应
        try:
            r = self.session.get(url,**kwargs)
            Logger().info(f"发送请求成功，{url}{kwargs},响应结果为{r.text}")
            return r
        except Exception as e:
            return Logger().error(f"发送请求异常，{url}{kwargs},异常信息为：{e}")

    def post(self,url,data=None,json=None,**kwargs):    #post请求
        try:
            r = self.session.post(url,data=data,json=json, **kwargs)
            Logger().info(f"发送请求成功，请求参数：{data or json} | 响应结果为{r.text}")
            return r
        except Exception as e:
            return Logger().error(f"发送请求失败，请求参数：{data or json} | 异常信息为{e}")

    def put(self,url,**kwargs):
        pass

    def delete(self):
        pass

if __name__ == '__main__':
    br = BaseRequests()
    br.get("http://www.baidu.com/s",params={"wd":"python"})
    print("-" * 100)
    br.get("http://127.0.0.1:8088")
    print("-"*100)
    br.get("http://127.0.0.1:8088/futureloan/mvc/api/member/register",
           params={"mobilephone": "18012345678", "pwd": "123456"})
    print("-"*100)
    br.post("http://127.0.0.1:8088/futureloan/mvc/api/member/login",
            data={"mobilephone": "18012345678", "pwd": "123456"})